<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <meta name="generator" content="VuePress 2.0.0-beta.61">
    <style>
      :root {
        --c-bg: #fff;
      }
      html.dark {
        --c-bg: #22272e;
      }
      html, body {
        background-color: var(--c-bg);
      }
    </style>
    <script>
      const userMode = localStorage.getItem('vuepress-color-scheme');
			const systemDarkMode = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
			if (userMode === 'dark' || (userMode !== 'light' && systemDarkMode)) {
				document.documentElement.classList.toggle('dark', true);
			}
    </script>
    <link type="image/svg" rel="icon" href="/press/favicon.svg"><meta name="keywords" content="前端,游戏,Vue,React,Node,前端项目规范化工具配置,Koa"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><meta name="robots" content="index,follow"><meta name="og:title" content="liu0716 的个人博客"><meta name="url" content="https://liu0716.gitee.io/press/"><meta name="og:url" content="https://lliu0716.github.io/press/"><title>使用 Vue3 canvas 实现经典游戏之贪吃蛇 | liu0716 的个人博客</title><meta name="description" content="Vue3,canvas,贪吃蛇,经典游戏,代码">
    <link rel="preload" href="/press/assets/style-bd81e949.css" as="style"><link rel="stylesheet" href="/press/assets/style-bd81e949.css">
    <link rel="modulepreload" href="/press/assets/app-133d66d8.js"><link rel="modulepreload" href="/press/assets/framework-c611892d.js"><link rel="modulepreload" href="/press/assets/index.html-ed5ef29f.js"><link rel="modulepreload" href="/press/assets/index.html-7c9b6bf6.js"><link rel="prefetch" href="/press/assets/index.html-1e68895e.js" as="script"><link rel="prefetch" href="/press/assets/index.html-cc64bccf.js" as="script"><link rel="prefetch" href="/press/assets/index.html-67a7af56.js" as="script"><link rel="prefetch" href="/press/assets/index.html-f93977f1.js" as="script"><link rel="prefetch" href="/press/assets/index.html-2a3cf2bb.js" as="script"><link rel="prefetch" href="/press/assets/index.html-5d2b61a8.js" as="script"><link rel="prefetch" href="/press/assets/index.html-df432437.js" as="script"><link rel="prefetch" href="/press/assets/index.html-f69a28a5.js" as="script"><link rel="prefetch" href="/press/assets/index.html-ce8b73c9.js" as="script"><link rel="prefetch" href="/press/assets/index.html-7f7e32ba.js" as="script"><link rel="prefetch" href="/press/assets/index.html-16a0b569.js" as="script"><link rel="prefetch" href="/press/assets/index.html-e42d9afb.js" as="script"><link rel="prefetch" href="/press/assets/index.html-978381eb.js" as="script"><link rel="prefetch" href="/press/assets/404.html-d1da943c.js" as="script"><link rel="prefetch" href="/press/assets/index.html-0c8973e5.js" as="script"><link rel="prefetch" href="/press/assets/index.html-43b21038.js" as="script"><link rel="prefetch" href="/press/assets/index.html-9ea21412.js" as="script"><link rel="prefetch" href="/press/assets/index.html-3bb6630f.js" as="script"><link rel="prefetch" href="/press/assets/index.html-8072adc9.js" as="script"><link rel="prefetch" href="/press/assets/index.html-7d6def65.js" as="script"><link rel="prefetch" href="/press/assets/index.html-2a9fae25.js" as="script"><link rel="prefetch" href="/press/assets/index.html-f431e102.js" as="script"><link rel="prefetch" href="/press/assets/index.html-f90bbd7e.js" as="script"><link rel="prefetch" href="/press/assets/index.html-f1b6c22e.js" as="script"><link rel="prefetch" href="/press/assets/index.html-67a8fefc.js" as="script"><link rel="prefetch" href="/press/assets/index.html-2371ace2.js" as="script"><link rel="prefetch" href="/press/assets/index.html-325348ac.js" as="script"><link rel="prefetch" href="/press/assets/404.html-de48043d.js" as="script"><link rel="prefetch" href="/press/assets/Alert-2c06a393.js" as="script"><link rel="prefetch" href="/press/assets/GluttonousSnake-32b623a0.js" as="script"><link rel="prefetch" href="/press/assets/GluttonousSnakeCanvas-a9be0618.js" as="script"><link rel="prefetch" href="/press/assets/Home-fa7ef25b.js" as="script"><link rel="prefetch" href="/press/assets/LinkList-5f3f0819.js" as="script"><link rel="prefetch" href="/press/assets/MineClearance-13791cfc.js" as="script"><link rel="prefetch" href="/press/assets/OtherComponent-bc557433.js" as="script"><link rel="prefetch" href="/press/assets/Control-9b65d1b7.js" as="script"><link rel="prefetch" href="/press/assets/Snake-b2035f7a.js" as="script"><link rel="prefetch" href="/press/assets/Mine-17f87f2b.js" as="script"><link rel="prefetch" href="/press/assets/setupDevtools-X4YFRK4B-d6697a55.js" as="script">
  </head>
  <body>
    <div id="app"><!--[--><div class="theme-container"><!--[--><header class="navbar"><div class="toggle-sidebar-button" title="toggle sidebar" aria-expanded="false" role="button" tabindex="0"><div class="icon" aria-hidden="true"><span></span><span></span><span></span></div></div><span><a href="/press/" class=""><img class="logo" src="/press/./favicon.svg" alt="liu0716 的个人博客"><span class="site-name can-hide">liu0716 的个人博客</span></a></span><div class="navbar-items-wrapper" style=""><!--[--><!--]--><nav class="navbar-items can-hide"><!--[--><div class="navbar-item"><a href="/press/" class="" aria-label="主页"><!--[--><!--]--> 主页 <!--[--><!--]--></a></div><div class="navbar-item"><a href="/press/web" class="" aria-label="web分享"><!--[--><!--]--> web分享 <!--[--><!--]--></a></div><div class="navbar-item"><a href="/press/node" class="" aria-label="node分享"><!--[--><!--]--> node分享 <!--[--><!--]--></a></div><div class="navbar-item"><a href="/press/love" class="" aria-label="前端与恋爱"><!--[--><!--]--> 前端与恋爱 <!--[--><!--]--></a></div><div class="navbar-item"><a href="/press/game" class="router-link-active" aria-label="游戏分享"><!--[--><!--]--> 游戏分享 <!--[--><!--]--></a></div><!--]--></nav><!--[--><!--]--><button class="toggle-color-mode-button" title="toggle color mode"><svg style="" class="icon" focusable="false" viewBox="0 0 32 32"><path d="M16 12.005a4 4 0 1 1-4 4a4.005 4.005 0 0 1 4-4m0-2a6 6 0 1 0 6 6a6 6 0 0 0-6-6z" fill="currentColor"></path><path d="M5.394 6.813l1.414-1.415l3.506 3.506L8.9 10.318z" fill="currentColor"></path><path d="M2 15.005h5v2H2z" fill="currentColor"></path><path d="M5.394 25.197L8.9 21.691l1.414 1.415l-3.506 3.505z" fill="currentColor"></path><path d="M15 25.005h2v5h-2z" fill="currentColor"></path><path d="M21.687 23.106l1.414-1.415l3.506 3.506l-1.414 1.414z" fill="currentColor"></path><path d="M25 15.005h5v2h-5z" fill="currentColor"></path><path d="M21.687 8.904l3.506-3.506l1.414 1.415l-3.506 3.505z" fill="currentColor"></path><path d="M15 2.005h2v5h-2z" fill="currentColor"></path></svg><svg style="display:none;" class="icon" focusable="false" viewBox="0 0 32 32"><path d="M13.502 5.414a15.075 15.075 0 0 0 11.594 18.194a11.113 11.113 0 0 1-7.975 3.39c-.138 0-.278.005-.418 0a11.094 11.094 0 0 1-3.2-21.584M14.98 3a1.002 1.002 0 0 0-.175.016a13.096 13.096 0 0 0 1.825 25.981c.164.006.328 0 .49 0a13.072 13.072 0 0 0 10.703-5.555a1.01 1.01 0 0 0-.783-1.565A13.08 13.08 0 0 1 15.89 4.38A1.015 1.015 0 0 0 14.98 3z" fill="currentColor"></path></svg></button><form class="search-box" role="search"><input type="search" autocomplete="off" spellcheck="false" value><!----></form></div></header><!--]--><div class="sidebar-mask"></div><!--[--><aside class="sidebar"><nav class="navbar-items"><!--[--><div class="navbar-item"><a href="/press/" class="" aria-label="主页"><!--[--><!--]--> 主页 <!--[--><!--]--></a></div><div class="navbar-item"><a href="/press/web" class="" aria-label="web分享"><!--[--><!--]--> web分享 <!--[--><!--]--></a></div><div class="navbar-item"><a href="/press/node" class="" aria-label="node分享"><!--[--><!--]--> node分享 <!--[--><!--]--></a></div><div class="navbar-item"><a href="/press/love" class="" aria-label="前端与恋爱"><!--[--><!--]--> 前端与恋爱 <!--[--><!--]--></a></div><div class="navbar-item"><a href="/press/game" class="router-link-active" aria-label="游戏分享"><!--[--><!--]--> 游戏分享 <!--[--><!--]--></a></div><!--]--></nav><!--[--><!--]--><ul class="sidebar-items"><!--[--><li><a href="/press/" class="sidebar-item sidebar-heading" aria-label="主页"><!--[--><!--]--> 主页 <!--[--><!--]--></a><!----></li><li><a href="/press/web" class="sidebar-item sidebar-heading" aria-label="web分享"><!--[--><!--]--> web分享 <!--[--><!--]--></a><ul style="" class="sidebar-item-children"><!--[--><li><a href="/press/web/tool" class="sidebar-item" aria-label="开发规范化工具配置"><!--[--><!--]--> 开发规范化工具配置 <!--[--><!--]--></a><!----></li><li><a href="/press/web/vue" class="sidebar-item" aria-label="Vue"><!--[--><!--]--> Vue <!--[--><!--]--></a><!----></li><li><a href="/press/web/react" class="sidebar-item" aria-label="React"><!--[--><!--]--> React <!--[--><!--]--></a><!----></li><!--]--></ul></li><li><a href="/press/node" class="sidebar-item sidebar-heading" aria-label="node分享"><!--[--><!--]--> node分享 <!--[--><!--]--></a><ul style="" class="sidebar-item-children"><!--[--><li><a href="/press/node" class="sidebar-item" aria-label="node"><!--[--><!--]--> node <!--[--><!--]--></a><!----></li><li><a href="/press/node/koa" class="sidebar-item" aria-label="Koa"><!--[--><!--]--> Koa <!--[--><!--]--></a><!----></li><!--]--></ul></li><li><a href="/press/love" class="sidebar-item sidebar-heading" aria-label="前端与恋爱"><!--[--><!--]--> 前端与恋爱 <!--[--><!--]--></a><!----></li><li><a href="/press/game" class="router-link-active sidebar-item sidebar-heading" aria-label="游戏分享"><!--[--><!--]--> 游戏分享 <!--[--><!--]--></a><!----></li><!--]--></ul><!--[--><!--]--></aside><!--]--><!--[--><main class="page"><!--[--><!--]--><div class="theme-default-content"><!--[--><!--]--><div><h1 id="使用-vue3-canvas-实现经典游戏之贪吃蛇-包含代码" tabindex="-1"><a class="header-anchor" href="#使用-vue3-canvas-实现经典游戏之贪吃蛇-包含代码" aria-hidden="true">#</a> 使用 Vue3 canvas 实现经典游戏之贪吃蛇 包含代码</h1><h2 id="经典游戏之贪吃蛇" tabindex="-1"><a class="header-anchor" href="#经典游戏之贪吃蛇" aria-hidden="true">#</a> 经典游戏之贪吃蛇</h2><!--[--><main class="main" data-v-82afbc54><div class="canvas-box" data-v-82afbc54><canvas id="canvas-snake" width="340" height="340" data-v-82afbc54></canvas></div><div class="main-count" data-v-82afbc54><button data-v-82afbc54>上一关</button><span data-v-82afbc54>当前长度 <b class="snakeList-length-0" data-v-82afbc54>0</b></span><button data-v-82afbc54>下一关</button></div><div class="main-control" data-v-82afbc54><button data-v-82afbc54>暂停</button><!----><button data-v-82afbc54>重来</button></div><p class="main-tip" data-v-82afbc54><b data-v-82afbc54>提示:</b> 长度超过 <span data-v-82afbc54>300</span> 自动加速 </p></main><!--teleport start--><!--teleport end--><!--]--><blockquote><p>细节上还可以更进一步, 这次先到这里了</p></blockquote><h2 id="完整代码" tabindex="-1"><a class="header-anchor" href="#完整代码" aria-hidden="true">#</a> 完整代码</h2><h3 id="游戏主页面" tabindex="-1"><a class="header-anchor" href="#游戏主页面" aria-hidden="true">#</a> 游戏主页面</h3><div class="language-vue line-numbers-mode" data-ext="vue"><pre class="language-vue"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span> <span class="token attr-name">setup</span> <span class="token attr-name">lang</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>ts<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token script"><span class="token language-javascript">
<span class="token keyword">import</span> <span class="token punctuation">{</span> onMounted<span class="token punctuation">,</span> ref<span class="token punctuation">,</span> computed <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">&#39;vue&#39;</span>

<span class="token keyword">import</span> Control <span class="token keyword">from</span> <span class="token string">&#39;./GluttonousSnake/Control.vue&#39;</span>
<span class="token keyword">import</span> Alert <span class="token keyword">from</span> <span class="token string">&#39;./Alert.vue&#39;</span>

type TSnake <span class="token operator">=</span> <span class="token punctuation">{</span>
  <span class="token literal-property property">x</span><span class="token operator">:</span> number
  <span class="token literal-property property">y</span><span class="token operator">:</span> number
  <span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">&#39;head&#39;</span> <span class="token operator">|</span> <span class="token string">&#39;body&#39;</span>
<span class="token punctuation">}</span>

<span class="token keyword">const</span> boxSize <span class="token operator">=</span> <span class="token number">17</span> <span class="token comment">// 大小</span>
<span class="token keyword">let</span> <span class="token literal-property property">timeId</span><span class="token operator">:</span> number
<span class="token doc-comment comment">/** 速度 */</span>
<span class="token keyword">let</span> speed <span class="token operator">=</span> <span class="token number">360</span>
<span class="token keyword">const</span> total <span class="token operator">=</span> <span class="token number">300</span> <span class="token comment">// 通关长度</span>

<span class="token keyword">const</span> config <span class="token operator">=</span> <span class="token punctuation">{</span>
  <span class="token doc-comment comment">/**
   * 方向信息
   * 1. ↑
   * 2: →
   * 3: ←
   * 4: ↓
   */</span>
  <span class="token literal-property property">direction</span><span class="token operator">:</span> <span class="token string">&#39;2&#39;</span>
<span class="token punctuation">}</span>

<span class="token keyword">const</span> canvasBox <span class="token operator">=</span> ref<span class="token operator">&lt;</span>HTMLCanvasElement<span class="token operator">&gt;</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">const</span> snakeList <span class="token operator">=</span> ref<span class="token operator">&lt;</span>TSnake<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token operator">&gt;</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token keyword">const</span> target <span class="token operator">=</span> ref<span class="token operator">&lt;</span>TSnake<span class="token operator">&gt;</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span> <span class="token keyword">as</span> TSnake<span class="token punctuation">)</span>
<span class="token doc-comment comment">/** 游戏 开始/暂停 */</span>
<span class="token keyword">const</span> suspend <span class="token operator">=</span> <span class="token function">ref</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span>

<span class="token keyword">const</span> ctx <span class="token operator">=</span> <span class="token function">computed</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> canvasBox<span class="token punctuation">.</span>value<span class="token operator">?.</span><span class="token function">getContext</span><span class="token punctuation">(</span><span class="token string">&#39;2d&#39;</span><span class="token punctuation">)</span> <span class="token keyword">as</span> CanvasRenderingContext2D
<span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token doc-comment comment">/** 点击方向按钮 */</span>
<span class="token keyword">const</span> <span class="token function-variable function">onClick</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter"><span class="token literal-property property">direction</span><span class="token operator">:</span> string</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>direction <span class="token operator">===</span> <span class="token string">&#39;Space&#39;</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">return</span>
  <span class="token punctuation">}</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>suspend<span class="token punctuation">.</span>value<span class="token punctuation">)</span> <span class="token keyword">return</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>config<span class="token punctuation">.</span>direction <span class="token operator">===</span> <span class="token string">&#39;1&#39;</span> <span class="token operator">&amp;&amp;</span> direction <span class="token operator">===</span> <span class="token string">&#39;4&#39;</span><span class="token punctuation">)</span> <span class="token keyword">return</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>config<span class="token punctuation">.</span>direction <span class="token operator">===</span> <span class="token string">&#39;2&#39;</span> <span class="token operator">&amp;&amp;</span> direction <span class="token operator">===</span> <span class="token string">&#39;3&#39;</span><span class="token punctuation">)</span> <span class="token keyword">return</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>config<span class="token punctuation">.</span>direction <span class="token operator">===</span> <span class="token string">&#39;3&#39;</span> <span class="token operator">&amp;&amp;</span> direction <span class="token operator">===</span> <span class="token string">&#39;2&#39;</span><span class="token punctuation">)</span> <span class="token keyword">return</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>config<span class="token punctuation">.</span>direction <span class="token operator">===</span> <span class="token string">&#39;4&#39;</span> <span class="token operator">&amp;&amp;</span> direction <span class="token operator">===</span> <span class="token string">&#39;1&#39;</span><span class="token punctuation">)</span> <span class="token keyword">return</span>
  config<span class="token punctuation">.</span>direction <span class="token operator">=</span> direction
  <span class="token comment">// move()</span>
<span class="token punctuation">}</span>

<span class="token keyword">function</span> <span class="token function">info</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  snakeList<span class="token punctuation">.</span>value <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
  <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">let</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator">&lt;=</span> <span class="token number">3</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    snakeList<span class="token punctuation">.</span>value<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
      <span class="token literal-property property">x</span><span class="token operator">:</span> i<span class="token punctuation">,</span>
      <span class="token literal-property property">y</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
      <span class="token literal-property property">type</span><span class="token operator">:</span> i <span class="token operator">===</span> <span class="token number">3</span> <span class="token operator">?</span> <span class="token string">&#39;head&#39;</span> <span class="token operator">:</span> <span class="token string">&#39;body&#39;</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
  <span class="token comment">// move()</span>
  <span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token doc-comment comment">/** 随机生成食物位置信息 */</span>
<span class="token keyword">const</span> randomNumber <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token parameter">any</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">const</span> x <span class="token operator">=</span> <span class="token operator">~</span><span class="token operator">~</span><span class="token punctuation">(</span>Math<span class="token punctuation">.</span><span class="token function">random</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token number">19</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">1</span>
  <span class="token keyword">const</span> y <span class="token operator">=</span> <span class="token operator">~</span><span class="token operator">~</span><span class="token punctuation">(</span>Math<span class="token punctuation">.</span><span class="token function">random</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token number">19</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">1</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>snakeList<span class="token punctuation">.</span>value<span class="token punctuation">.</span><span class="token function">some</span><span class="token punctuation">(</span><span class="token parameter">v</span> <span class="token operator">=&gt;</span> v<span class="token punctuation">.</span>x <span class="token operator">===</span> x <span class="token operator">&amp;&amp;</span> v<span class="token punctuation">.</span>y <span class="token operator">===</span> y<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token punctuation">{</span> x<span class="token punctuation">,</span> y <span class="token punctuation">}</span>
  <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token function">randomNumber</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token doc-comment comment">/** 生成食物 */</span>
<span class="token keyword">const</span> <span class="token function-variable function">randomTarget</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">check <span class="token operator">=</span> <span class="token boolean">false</span></span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>check <span class="token operator">||</span> <span class="token operator">!</span>target<span class="token punctuation">.</span>value<span class="token punctuation">.</span>type<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">const</span> <span class="token punctuation">{</span> x<span class="token punctuation">,</span> y <span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token function">randomNumber</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    target<span class="token punctuation">.</span>value <span class="token operator">=</span> <span class="token punctuation">{</span>
      <span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">&#39;body&#39;</span><span class="token punctuation">,</span>
      y<span class="token punctuation">,</span>
      x
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
  ctx<span class="token punctuation">.</span>value<span class="token punctuation">.</span><span class="token function">beginPath</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  ctx<span class="token punctuation">.</span>value<span class="token punctuation">.</span>fillStyle <span class="token operator">=</span> <span class="token string">&#39;#ff0&#39;</span>
  ctx<span class="token punctuation">.</span>value<span class="token punctuation">.</span><span class="token function">fillRect</span><span class="token punctuation">(</span>
    <span class="token punctuation">(</span>target<span class="token punctuation">.</span>value<span class="token punctuation">.</span>x <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">*</span> boxSize<span class="token punctuation">,</span>
    <span class="token punctuation">(</span>target<span class="token punctuation">.</span>value<span class="token punctuation">.</span>y <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">*</span> boxSize<span class="token punctuation">,</span>
    boxSize<span class="token punctuation">,</span>
    boxSize
  <span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token keyword">function</span> <span class="token function">drawSnake</span><span class="token punctuation">(</span><span class="token parameter"><span class="token literal-property property">snakeList</span><span class="token operator">:</span> TSnake<span class="token punctuation">[</span><span class="token punctuation">]</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  snakeList<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token parameter">v</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    ctx<span class="token punctuation">.</span>value<span class="token punctuation">.</span><span class="token function">beginPath</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">const</span> x <span class="token operator">=</span> <span class="token punctuation">(</span>v<span class="token punctuation">.</span>x <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">*</span> boxSize
    <span class="token keyword">const</span> y <span class="token operator">=</span> <span class="token punctuation">(</span>v<span class="token punctuation">.</span>y <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">*</span> boxSize
    <span class="token keyword">if</span> <span class="token punctuation">(</span>v<span class="token punctuation">.</span>type <span class="token operator">===</span> <span class="token string">&#39;head&#39;</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      ctx<span class="token punctuation">.</span>value<span class="token punctuation">.</span>fillStyle <span class="token operator">=</span> <span class="token string">&#39;red&#39;</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
      ctx<span class="token punctuation">.</span>value<span class="token punctuation">.</span>fillStyle <span class="token operator">=</span> <span class="token string">&#39;green&#39;</span>
    <span class="token punctuation">}</span>
    ctx<span class="token punctuation">.</span>value<span class="token punctuation">.</span><span class="token function">fillRect</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> y<span class="token punctuation">,</span> boxSize<span class="token punctuation">,</span> boxSize<span class="token punctuation">)</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token keyword">function</span> <span class="token function">getSnake</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">const</span> <span class="token literal-property property">_snake</span><span class="token operator">:</span> TSnake<span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
  <span class="token keyword">const</span> item <span class="token operator">=</span> snakeList<span class="token punctuation">.</span>value
  <span class="token keyword">const</span> head <span class="token operator">=</span> item<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token parameter">v</span> <span class="token operator">=&gt;</span> v<span class="token punctuation">.</span>type <span class="token operator">===</span> <span class="token string">&#39;head&#39;</span><span class="token punctuation">)</span>
  <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">let</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> item<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    item<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>type <span class="token operator">=</span> <span class="token string">&#39;body&#39;</span>
    _snake<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>item<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
  <span class="token keyword">const</span> <span class="token literal-property property">obj</span><span class="token operator">:</span> TSnake <span class="token operator">=</span> <span class="token punctuation">{</span>
    <span class="token literal-property property">x</span><span class="token operator">:</span> head<span class="token operator">?.</span>x<span class="token operator">!</span><span class="token punctuation">,</span>
    <span class="token literal-property property">y</span><span class="token operator">:</span> head<span class="token operator">?.</span>y<span class="token operator">!</span><span class="token punctuation">,</span>
    <span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">&#39;head&#39;</span>
  <span class="token punctuation">}</span>
  <span class="token keyword">switch</span> <span class="token punctuation">(</span>config<span class="token punctuation">.</span>direction<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">case</span> <span class="token string">&#39;1&#39;</span><span class="token operator">:</span>
      obj<span class="token punctuation">.</span>y <span class="token operator">-=</span> <span class="token number">1</span>
      <span class="token keyword">break</span>
    <span class="token keyword">case</span> <span class="token string">&#39;2&#39;</span><span class="token operator">:</span>
      obj<span class="token punctuation">.</span>x <span class="token operator">+=</span> <span class="token number">1</span>
      <span class="token keyword">break</span>
    <span class="token keyword">case</span> <span class="token string">&#39;3&#39;</span><span class="token operator">:</span>
      obj<span class="token punctuation">.</span>x <span class="token operator">-=</span> <span class="token number">1</span>
      <span class="token keyword">break</span>
    <span class="token keyword">default</span><span class="token operator">:</span>
      obj<span class="token punctuation">.</span>y <span class="token operator">+=</span> <span class="token number">1</span>
      <span class="token keyword">break</span>
  <span class="token punctuation">}</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>
    obj<span class="token punctuation">.</span>x <span class="token operator">&gt;</span> boxSize <span class="token operator">+</span> <span class="token number">3</span> <span class="token operator">||</span>
    obj<span class="token punctuation">.</span>x <span class="token operator">&lt;</span> <span class="token number">1</span> <span class="token operator">||</span>
    obj<span class="token punctuation">.</span>y <span class="token operator">&gt;</span> boxSize <span class="token operator">+</span> <span class="token number">3</span> <span class="token operator">||</span>
    obj<span class="token punctuation">.</span>y <span class="token operator">&lt;</span> <span class="token number">1</span> <span class="token operator">||</span>
    snakeList<span class="token punctuation">.</span>value<span class="token punctuation">.</span><span class="token function">some</span><span class="token punctuation">(</span><span class="token parameter">v</span> <span class="token operator">=&gt;</span> v<span class="token punctuation">.</span>x <span class="token operator">===</span> obj<span class="token punctuation">.</span>x <span class="token operator">&amp;&amp;</span> v<span class="token punctuation">.</span>y <span class="token operator">===</span> obj<span class="token punctuation">.</span>y<span class="token punctuation">)</span>
  <span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token function">info</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token function">randomTarget</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span>
    <span class="token function">clearInterval</span><span class="token punctuation">(</span>timeId<span class="token punctuation">)</span>
    isShow<span class="token punctuation">.</span>value <span class="token operator">=</span> <span class="token boolean">true</span>
    <span class="token keyword">return</span>
  <span class="token punctuation">}</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>obj<span class="token punctuation">.</span>x <span class="token operator">===</span> target<span class="token punctuation">.</span>value<span class="token punctuation">.</span>x <span class="token operator">&amp;&amp;</span> obj<span class="token punctuation">.</span>y <span class="token operator">===</span> target<span class="token punctuation">.</span>value<span class="token punctuation">.</span>y<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    _snake<span class="token punctuation">.</span><span class="token function">unshift</span><span class="token punctuation">(</span>target<span class="token punctuation">.</span>value<span class="token punctuation">)</span>
    <span class="token function">randomTarget</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
  _snake<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span>
  snakeList<span class="token punctuation">.</span>value <span class="token operator">=</span> _snake
  <span class="token function">drawSnake</span><span class="token punctuation">(</span>snakeList<span class="token punctuation">.</span>value<span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token keyword">function</span> <span class="token function">drawBoard</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">let</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> <span class="token number">20</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    ctx<span class="token punctuation">.</span>value<span class="token punctuation">.</span><span class="token function">beginPath</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    ctx<span class="token punctuation">.</span>value<span class="token punctuation">.</span><span class="token function">moveTo</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> i <span class="token operator">*</span> boxSize<span class="token punctuation">)</span>
    ctx<span class="token punctuation">.</span>value<span class="token punctuation">.</span><span class="token function">lineTo</span><span class="token punctuation">(</span><span class="token number">800</span><span class="token punctuation">,</span> i <span class="token operator">*</span> boxSize<span class="token punctuation">)</span>
    ctx<span class="token punctuation">.</span>value<span class="token punctuation">.</span><span class="token function">stroke</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    ctx<span class="token punctuation">.</span>value<span class="token punctuation">.</span><span class="token function">beginPath</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    ctx<span class="token punctuation">.</span>value<span class="token punctuation">.</span><span class="token function">moveTo</span><span class="token punctuation">(</span>i <span class="token operator">*</span> boxSize<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span>
    ctx<span class="token punctuation">.</span>value<span class="token punctuation">.</span><span class="token function">lineTo</span><span class="token punctuation">(</span>i <span class="token operator">*</span> boxSize<span class="token punctuation">,</span> <span class="token number">800</span><span class="token punctuation">)</span>
    ctx<span class="token punctuation">.</span>value<span class="token punctuation">.</span><span class="token function">stroke</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">function</span> <span class="token function">move</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token function">clearInterval</span><span class="token punctuation">(</span>timeId<span class="token punctuation">)</span>
  timeId <span class="token operator">=</span> <span class="token function">setInterval</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    ctx<span class="token punctuation">.</span>value<span class="token punctuation">.</span><span class="token function">clearRect</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">600</span><span class="token punctuation">,</span> <span class="token number">600</span><span class="token punctuation">)</span>
    <span class="token comment">// drawBoard()</span>
    <span class="token function">getSnake</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token function">drawSnake</span><span class="token punctuation">(</span>snakeList<span class="token punctuation">.</span>value<span class="token punctuation">)</span>
    <span class="token function">randomTarget</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span>
    <span class="token comment">// move()</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span> speed<span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token keyword">function</span> <span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  suspend<span class="token punctuation">.</span>value <span class="token operator">=</span> <span class="token operator">!</span>suspend<span class="token punctuation">.</span>value
  suspend<span class="token punctuation">.</span>value <span class="token operator">?</span> <span class="token function">clearInterval</span><span class="token punctuation">(</span>timeId<span class="token punctuation">)</span> <span class="token operator">:</span> <span class="token function">move</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token doc-comment comment">/** 加速或者减速 */</span>
<span class="token keyword">const</span> <span class="token function-variable function">select</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter"><span class="token literal-property property">up</span><span class="token operator">:</span> boolean</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>speed <span class="token operator">&gt;</span> <span class="token number">80</span> <span class="token operator">&amp;&amp;</span> up<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    speed <span class="token operator">-=</span> <span class="token number">60</span>
  <span class="token punctuation">}</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>speed <span class="token operator">&lt;</span> <span class="token number">800</span> <span class="token operator">&amp;&amp;</span> <span class="token operator">!</span>up<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    speed <span class="token operator">+=</span> <span class="token number">60</span>
  <span class="token punctuation">}</span>
  <span class="token function">info</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token keyword">const</span> isShow <span class="token operator">=</span> <span class="token function">ref</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span>

<span class="token keyword">function</span> <span class="token function">onConfirm</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  suspend<span class="token punctuation">.</span>value <span class="token operator">=</span> <span class="token boolean">false</span>
  isShow<span class="token punctuation">.</span>value <span class="token operator">=</span> <span class="token boolean">false</span>
  config<span class="token punctuation">.</span>direction <span class="token operator">=</span> <span class="token string">&#39;2&#39;</span>
  <span class="token function">info</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token function">onMounted</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token function">drawBoard</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  <span class="token function">info</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  <span class="token function">drawSnake</span><span class="token punctuation">(</span>snakeList<span class="token punctuation">.</span>value<span class="token punctuation">)</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">&gt;</span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>template</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>main</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>main<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>canvas-box<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>canvas</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>canvas-snake<span class="token punctuation">&quot;</span></span> <span class="token attr-name">ref</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>canvasBox<span class="token punctuation">&quot;</span></span> <span class="token attr-name">width</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>340<span class="token punctuation">&quot;</span></span> <span class="token attr-name">height</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>340<span class="token punctuation">&quot;</span></span> <span class="token punctuation">/&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>main-count<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>button</span> <span class="token attr-name">@click</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>select(false)<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>上一关<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>button</span><span class="token punctuation">&gt;</span></span>
      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>span</span>
        <span class="token punctuation">&gt;</span></span>当前长度
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>b</span> <span class="token attr-name">:class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>`snakeList-length-${snakeList.length % 3}`<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
          {{ snakeList.length }}
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>b</span><span class="token punctuation">&gt;</span></span>
      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>span</span><span class="token punctuation">&gt;</span></span>
      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>button</span> <span class="token attr-name">@click</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>select(true)<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>下一关<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>button</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>main-control<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>button</span> <span class="token attr-name">@click</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>start<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>{{ !suspend ? &#39;暂停&#39; : &#39;开始&#39; }}<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>button</span><span class="token punctuation">&gt;</span></span>
      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>Control</span> <span class="token attr-name">@on-click</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>onClick<span class="token punctuation">&quot;</span></span> <span class="token punctuation">/&gt;</span></span>
      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>button</span> <span class="token attr-name">@click</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>info<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>重来<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>button</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>main-tip<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>b</span><span class="token punctuation">&gt;</span></span>提示:<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>b</span><span class="token punctuation">&gt;</span></span> 长度超过
      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>span</span><span class="token punctuation">&gt;</span></span>{{ total }}<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>span</span><span class="token punctuation">&gt;</span></span>
      自动加速
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>main</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>Alert</span> <span class="token attr-name"><span class="token namespace">v-model:</span>value</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>isShow<span class="token punctuation">&quot;</span></span> <span class="token attr-name">@close</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>onConfirm<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>alert<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>b</span><span class="token punctuation">&gt;</span></span>游戏失败<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>b</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>Alert</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>template</span><span class="token punctuation">&gt;</span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>style</span> <span class="token attr-name">scoped</span> <span class="token attr-name">lang</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>less<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token style"><span class="token language-css">
<span class="token selector">.alert</span> <span class="token punctuation">{</span>
  <span class="token property">text-align</span><span class="token punctuation">:</span> center<span class="token punctuation">;</span>
  <span class="token property">color</span><span class="token punctuation">:</span> #000<span class="token punctuation">;</span>
  <span class="token property">margin-top</span><span class="token punctuation">:</span> 20px<span class="token punctuation">;</span>
  <span class="token property">font-size</span><span class="token punctuation">:</span> 22px<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token atrule"><span class="token rule">@media</span> <span class="token punctuation">(</span><span class="token property">max-width</span><span class="token punctuation">:</span> 419px<span class="token punctuation">)</span></span> <span class="token punctuation">{</span>
  <span class="token selector">.main</span> <span class="token punctuation">{</span>
    <span class="token property">position</span><span class="token punctuation">:</span> relative<span class="token punctuation">;</span>
    <span class="token property">left</span><span class="token punctuation">:</span> 50%<span class="token punctuation">;</span>
    <span class="token property">transform</span><span class="token punctuation">:</span> <span class="token function">translateX</span><span class="token punctuation">(</span>-50%<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token selector">.main</span> <span class="token punctuation">{</span>
  <span class="token property">width</span><span class="token punctuation">:</span> 100%<span class="token punctuation">;</span>
  <span class="token property">max-width</span><span class="token punctuation">:</span> 375px<span class="token punctuation">;</span>
  <span class="token property">min-width</span><span class="token punctuation">:</span> 375px<span class="token punctuation">;</span>
  <span class="token property">margin</span><span class="token punctuation">:</span> 10px auto<span class="token punctuation">;</span>
  <span class="token property">overflow</span><span class="token punctuation">:</span> hidden<span class="token punctuation">;</span>

  <span class="token selector">.canvas-box</span> <span class="token punctuation">{</span>
    <span class="token property">width</span><span class="token punctuation">:</span> 100%<span class="token punctuation">;</span>
    <span class="token property">display</span><span class="token punctuation">:</span> flex<span class="token punctuation">;</span>
    <span class="token property">justify-content</span><span class="token punctuation">:</span> center<span class="token punctuation">;</span>
    <span class="token property">align-items</span><span class="token punctuation">:</span> center<span class="token punctuation">;</span>
    <span class="token property">margin</span><span class="token punctuation">:</span> 12px 0<span class="token punctuation">;</span>

    <span class="token selector">canvas</span> <span class="token punctuation">{</span>
      <span class="token property">border</span><span class="token punctuation">:</span> 1px solid #000<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>

  <span class="token selector">&amp;-count</span> <span class="token punctuation">{</span>
    <span class="token property">display</span><span class="token punctuation">:</span> flex<span class="token punctuation">;</span>
    <span class="token property">align-items</span><span class="token punctuation">:</span> center<span class="token punctuation">;</span>
    <span class="token property">justify-content</span><span class="token punctuation">:</span> space-around<span class="token punctuation">;</span>
    <span class="token property">margin</span><span class="token punctuation">:</span> 20px auto<span class="token punctuation">;</span>

    <span class="token selector">.snakeList-length-0</span> <span class="token punctuation">{</span>
      <span class="token property">color</span><span class="token punctuation">:</span> #00f<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token selector">.snakeList-length-1</span> <span class="token punctuation">{</span>
      <span class="token property">color</span><span class="token punctuation">:</span> #0f0<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token selector">.snakeList-length-2</span> <span class="token punctuation">{</span>
      <span class="token property">color</span><span class="token punctuation">:</span> #f00<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token selector">button</span> <span class="token punctuation">{</span>
      <span class="token property">padding</span><span class="token punctuation">:</span> 8px 16px<span class="token punctuation">;</span>
      <span class="token property">border-radius</span><span class="token punctuation">:</span> 4px<span class="token punctuation">;</span>
      <span class="token property">outline</span><span class="token punctuation">:</span> none<span class="token punctuation">;</span>
      <span class="token property">border</span><span class="token punctuation">:</span> none<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>

  <span class="token selector">&amp;-control</span> <span class="token punctuation">{</span>
    <span class="token property">display</span><span class="token punctuation">:</span> flex<span class="token punctuation">;</span>
    <span class="token property">align-items</span><span class="token punctuation">:</span> center<span class="token punctuation">;</span>
    <span class="token property">justify-content</span><span class="token punctuation">:</span> space-around<span class="token punctuation">;</span>

    <span class="token selector">button</span> <span class="token punctuation">{</span>
      <span class="token property">padding</span><span class="token punctuation">:</span> 12px 20px<span class="token punctuation">;</span>
      <span class="token property">border-radius</span><span class="token punctuation">:</span> 4px<span class="token punctuation">;</span>
      <span class="token property">outline</span><span class="token punctuation">:</span> none<span class="token punctuation">;</span>
      <span class="token property">border</span><span class="token punctuation">:</span> none<span class="token punctuation">;</span>
      <span class="token property">font-size</span><span class="token punctuation">:</span> 20px<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>

  <span class="token selector">&amp;-tip</span> <span class="token punctuation">{</span>
    <span class="token property">text-align</span><span class="token punctuation">:</span> center<span class="token punctuation">;</span>
    <span class="token property">font-size</span><span class="token punctuation">:</span> 20px<span class="token punctuation">;</span>
    <span class="token property">margin-top</span><span class="token punctuation">:</span> 30px<span class="token punctuation">;</span>

    <span class="token selector">&gt; span</span> <span class="token punctuation">{</span>
      <span class="token property">color</span><span class="token punctuation">:</span> #f00<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>style</span><span class="token punctuation">&gt;</span></span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="控制页面" tabindex="-1"><a class="header-anchor" href="#控制页面" aria-hidden="true">#</a> 控制页面</h3><div class="language-vue line-numbers-mode" data-ext="vue"><pre class="language-vue"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span> <span class="token attr-name">setup</span> <span class="token attr-name">lang</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>ts<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token script"><span class="token language-javascript">
<span class="token keyword">import</span> <span class="token punctuation">{</span> ref<span class="token punctuation">,</span> onMounted<span class="token punctuation">,</span> onBeforeUnmount <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">&#39;vue&#39;</span>
<span class="token keyword">import</span> base <span class="token keyword">from</span> <span class="token string">&#39;../base&#39;</span>

<span class="token keyword">const</span> emits <span class="token operator">=</span> <span class="token function">defineEmits</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">&#39;onClick&#39;</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token keyword">const</span> touchstart <span class="token operator">=</span> <span class="token function">ref</span><span class="token punctuation">(</span><span class="token string">&#39;&#39;</span><span class="token punctuation">)</span>

<span class="token keyword">const</span> <span class="token punctuation">{</span> isH5 <span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token function">base</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

<span class="token keyword">const</span> <span class="token function-variable function">addEventListener</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  window<span class="token punctuation">.</span><span class="token function">addEventListener</span><span class="token punctuation">(</span><span class="token string">&#39;keydown&#39;</span><span class="token punctuation">,</span> <span class="token parameter">e</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token keyword">let</span> key <span class="token operator">=</span> <span class="token string">&#39;2&#39;</span>
    <span class="token keyword">switch</span> <span class="token punctuation">(</span>e<span class="token punctuation">.</span>code<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">case</span> <span class="token string">&#39;Space&#39;</span><span class="token operator">:</span>
        key <span class="token operator">=</span> <span class="token string">&#39;Space&#39;</span>
        <span class="token keyword">break</span>
      <span class="token keyword">case</span> <span class="token string">&#39;ArrowUp&#39;</span><span class="token operator">:</span>
      <span class="token keyword">case</span> <span class="token string">&#39;KeyW&#39;</span><span class="token operator">:</span>
        key <span class="token operator">=</span> <span class="token string">&#39;1&#39;</span>
        <span class="token keyword">break</span>
      <span class="token keyword">case</span> <span class="token string">&#39;ArrowDown&#39;</span><span class="token operator">:</span>
      <span class="token keyword">case</span> <span class="token string">&#39;KeyS&#39;</span><span class="token operator">:</span>
        key <span class="token operator">=</span> <span class="token string">&#39;4&#39;</span>
        <span class="token keyword">break</span>
      <span class="token keyword">case</span> <span class="token string">&#39;ArrowLeft&#39;</span><span class="token operator">:</span>
      <span class="token keyword">case</span> <span class="token string">&#39;KeyA&#39;</span><span class="token operator">:</span>
        key <span class="token operator">=</span> <span class="token string">&#39;3&#39;</span>
        <span class="token keyword">break</span>
      <span class="token keyword">default</span><span class="token operator">:</span>
        key <span class="token operator">=</span> <span class="token string">&#39;2&#39;</span>
        <span class="token keyword">break</span>
    <span class="token punctuation">}</span>
    <span class="token function">emits</span><span class="token punctuation">(</span><span class="token string">&#39;onClick&#39;</span><span class="token punctuation">,</span> key<span class="token punctuation">)</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token keyword">const</span> <span class="token function-variable function">onClick</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter"><span class="token literal-property property">target</span><span class="token operator">:</span> HTMLElement</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  touchstart<span class="token punctuation">.</span>value <span class="token operator">=</span> target<span class="token punctuation">.</span>innerText
  <span class="token function">emits</span><span class="token punctuation">(</span><span class="token string">&#39;onClick&#39;</span><span class="token punctuation">,</span> target<span class="token punctuation">.</span>innerText<span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token function">onMounted</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>isH5<span class="token punctuation">.</span>value<span class="token punctuation">)</span> <span class="token keyword">return</span>
  <span class="token function">addEventListener</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token function">onBeforeUnmount</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  window<span class="token punctuation">.</span><span class="token function">removeEventListener</span><span class="token punctuation">(</span><span class="token string">&#39;keydown&#39;</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">&gt;</span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>template</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span>
    <span class="token attr-name">v-if</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>isH5<span class="token punctuation">&quot;</span></span>
    <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>control<span class="token punctuation">&quot;</span></span>
    <span class="token attr-name">@touchstart</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>e =&gt; onClick(e.target as HTMLElement)<span class="token punctuation">&quot;</span></span>
    <span class="token attr-name">@touchend</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>() =&gt; (touchstart = &#39;&#39;)<span class="token punctuation">&quot;</span></span>
  <span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>span</span> <span class="token attr-name">v-for</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>v in 4<span class="token punctuation">&quot;</span></span> <span class="token attr-name">:class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>[touchstart === `${v}` ? &#39;check&#39; : &#39;&#39;]<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
      {{ v }}
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>span</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>template</span><span class="token punctuation">&gt;</span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>style</span> <span class="token attr-name">scoped</span> <span class="token attr-name">lang</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>less<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token style"><span class="token language-css">
<span class="token selector">.control</span> <span class="token punctuation">{</span>
  <span class="token property">border</span><span class="token punctuation">:</span> 1px solid #000<span class="token punctuation">;</span>
  <span class="token property">width</span><span class="token punctuation">:</span> 140px<span class="token punctuation">;</span>
  <span class="token property">height</span><span class="token punctuation">:</span> 140px<span class="token punctuation">;</span>
  <span class="token property">overflow</span><span class="token punctuation">:</span> hidden<span class="token punctuation">;</span>
  <span class="token property">border-radius</span><span class="token punctuation">:</span> 50%<span class="token punctuation">;</span>
  <span class="token property">display</span><span class="token punctuation">:</span> flex<span class="token punctuation">;</span>
  <span class="token property">flex-wrap</span><span class="token punctuation">:</span> wrap<span class="token punctuation">;</span>
  <span class="token property">transform</span><span class="token punctuation">:</span> <span class="token function">rotate</span><span class="token punctuation">(</span>45deg<span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token selector">&gt; span</span> <span class="token punctuation">{</span>
    <span class="token property">outline</span><span class="token punctuation">:</span> 1px solid #000<span class="token punctuation">;</span>
    <span class="token property">width</span><span class="token punctuation">:</span> 50%<span class="token punctuation">;</span>
    <span class="token property">height</span><span class="token punctuation">:</span> 50%<span class="token punctuation">;</span>
    <span class="token property">font-size</span><span class="token punctuation">:</span> 0<span class="token punctuation">;</span>

    <span class="token selector">&amp;.check</span> <span class="token punctuation">{</span>
      <span class="token property">background-color</span><span class="token punctuation">:</span> aquamarine<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>style</span><span class="token punctuation">&gt;</span></span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="页面监听逻辑" tabindex="-1"><a class="header-anchor" href="#页面监听逻辑" aria-hidden="true">#</a> 页面监听逻辑</h3><div class="language-typescript line-numbers-mode" data-ext="ts"><pre class="language-typescript"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> ref<span class="token punctuation">,</span> watch<span class="token punctuation">,</span> onMounted <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">&#39;vue&#39;</span>

<span class="token keyword">const</span> <span class="token function-variable function">bast</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">const</span> resize <span class="token operator">=</span> <span class="token function">ref</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span>
  <span class="token keyword">const</span> isH5 <span class="token operator">=</span> <span class="token function">ref</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span>
  <span class="token keyword">let</span> timeId <span class="token operator">=</span> <span class="token boolean">true</span>

  <span class="token function">onMounted</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token comment">// 窗口发生改变才能触发监听 , 所以先让监听的值发生改变</span>
    resize<span class="token punctuation">.</span>value <span class="token operator">=</span> window<span class="token punctuation">.</span>innerWidth
    window<span class="token punctuation">.</span><span class="token function-variable function">onresize</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
      <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>timeId<span class="token punctuation">)</span> <span class="token punctuation">{</span>
          timeId <span class="token operator">=</span> <span class="token boolean">false</span>
          <span class="token keyword">return</span> window<span class="token punctuation">.</span><span class="token function">setTimeout</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
            resize<span class="token punctuation">.</span>value <span class="token operator">=</span> window<span class="token punctuation">.</span>innerWidth
            timeId <span class="token operator">=</span> <span class="token boolean">true</span>
          <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">300</span><span class="token punctuation">)</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">return</span> <span class="token keyword">null</span>
      <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span>

  <span class="token comment">// 这样可以关闭监听</span>
  <span class="token keyword">const</span> outWatch <span class="token operator">=</span> <span class="token function">watch</span><span class="token punctuation">(</span>resize<span class="token punctuation">,</span> value <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token keyword">const</span> isAndroid <span class="token operator">=</span> window<span class="token punctuation">.</span>navigator<span class="token punctuation">.</span>userAgent<span class="token punctuation">.</span><span class="token function">match</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">android</span><span class="token regex-delimiter">/</span><span class="token regex-flags">gi</span></span><span class="token punctuation">)</span>
    <span class="token keyword">const</span> isIPhone <span class="token operator">=</span> window<span class="token punctuation">.</span>navigator<span class="token punctuation">.</span>userAgent<span class="token punctuation">.</span><span class="token function">match</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">iphone</span><span class="token regex-delimiter">/</span><span class="token regex-flags">gi</span></span><span class="token punctuation">)</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>isIPhone <span class="token operator">||</span> isAndroid <span class="token operator">||</span> value <span class="token operator">&lt;=</span> <span class="token number">750</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      isH5<span class="token punctuation">.</span>value <span class="token operator">=</span> <span class="token boolean">true</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
      isH5<span class="token punctuation">.</span>value <span class="token operator">=</span> <span class="token boolean">false</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span>

  <span class="token keyword">return</span> <span class="token punctuation">{</span>
    <span class="token comment">// 监听窗口大小</span>
    isH5<span class="token punctuation">,</span>
    <span class="token comment">// 关闭监听</span>
    outWatch
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">export</span> <span class="token keyword">default</span> bast
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div></div><!--[--><!--]--></div><footer class="page-meta"><!----><div class="meta-item last-updated"><span class="meta-item-label">最近更新时间: </span><!----></div><div class="meta-item contributors"><span class="meta-item-label">Contributors: </span><span class="meta-item-info"><!--[--><!--[--><span class="contributor" title="email: 450441058@qq.com">liu</span><!----><!--]--><!--]--></span></div></footer><!----><!--[--><!--]--></main><!--]--></div><!----><!--]--></div>
    <script type="module" src="/press/assets/app-133d66d8.js" defer></script>
  </body>
</html>
